package com.example.black.myapplication.uiflow;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2019/01/18/11:27
 *     desc   : 标签布局
 *     version: 1.0
 * </pre>
 */

public class TagLayout extends ViewGroup {
    List<Integer> lineHeights = new ArrayList<>();//每一行的最大控件高度
    List<List<View>> views = new ArrayList<>();//每一行的控件list
    public TagLayout(Context context) {
        super(context);
    }

    public TagLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TagLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        views.clear();//清理无关的内容
        lineHeights.clear();//清理无关内容
        List<View> lineViews = new ArrayList<>();//定义空list装view
        int width = getMeasuredWidth();//获取容器的宽度
        int lineWidth = 0;//定义line宽度
        int lineHeight = 0;//定义line高度
        int childCount = getChildCount();
        for(int i = 0;i < childCount;i++){
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            //超出换行
            if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width){//判断一个孩子的宽和margin+之前的宽度大于之前的宽度
                lineHeights.add(lineHeight);
                views.add(lineViews);
                lineWidth = 0;
                lineViews = new ArrayList<>();
            }

            lineWidth += childWidth +lp.leftMargin+lp.rightMargin;
            lineHeight = Math.max(lineHeight,childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        lineHeights.add(lineHeight);
        views.add(lineViews);

        int left = 0;//定位控件左边
        int top = 0;//定位控件头部
        //摆放
        int size = views.size();
        for (int i = 0; i < size; i++ ){
            lineViews = views.get(i);
            lineHeight = lineHeights.get(i);
            for (int j = 0; j < lineViews.size(); j++){
                View child = lineViews.get(j);
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc = lc + child.getMeasuredWidth();
                int bc = tc + child.getMeasuredHeight();
                child.layout(lc,tc,rc,bc);
                left += child.getMeasuredWidth() + lp.leftMargin+lp.rightMargin;
            }
            left = 0;
            top += lineHeight;//利用获取到之前存储的高度，累加向下摆放
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int height = 0;
        int width = 0;

        int lineWidth = 0;//临时存放一行的宽度
        int lineHeight = 0 ;//临时存放一行高度
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++ ){
            View child = getChildAt(i);
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if ( lineWidth + childWidth > widthSize){//换行
                width = Math.max(lineWidth,width);
                lineWidth = childWidth;
                height += lineHeight;
                lineHeight = childHeight;

            }else { //累加摆放
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight,childHeight);
            }

            if (i == childCount - 1){
                width = Math.max(width,lineWidth);
                height += lineHeight; //累加最后一个控件的高度
            }
        }

        int measureWidth = (widthMode == MeasureSpec.EXACTLY)? widthSize:width;
        int measureHeight = (heightMode == MeasureSpec.EXACTLY)? heightSize:height;
        setMeasuredDimension(measureWidth,measureHeight);
    }

    /**
     *  用来给child的layout用的？？
     * @param p
     * @return
     */
    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    /**
     *  用来给child的layout用的？？
     * @param attrs
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }


}
